package com.zengcheng.androidmvpweather.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zengcheng.androidmvpweather.R;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    //声明变量
    private Handler mHandler;
    EditText account;
    EditText password;
    private Button loginButton ;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        account =(EditText) findViewById(R.id.loginAccount_id);
        password=(EditText) findViewById(R.id.password_id);
        loginButton=(Button) findViewById(R.id.login);
        // 对登录按钮设置监听，方法由下面的Onclick实现
        loginButton.setOnClickListener(this);
    }
    @Override
    /**
     * 实现登录按钮的跳转
     */
    public void onClick(View v) {
        // 根据id判断单击的是哪个控件，固定写法
        switch (v.getId()) {
            case R.id.login:
                login();
                break;
            default:
                break;
        }
    }
    /**
     * 登录方法
     */
    public boolean login() {
        String userName=account.getText().toString().trim();
        String pwd=password.getText().toString().trim();
        if (account.equals("zengcheng")&& pwd.equals(123456)) {
            //实现界面的跳转
            Intent intent = new Intent(LoginActivity.this,WeatherActivity.class);
            startActivity(intent);
            //关闭当前界面
            finish();
        }else{
            Toast.makeText(LoginActivity.this, "用户名或密码错误",Toast.LENGTH_SHORT).show();
                    }

            //主线程


        return false;
    }
    /**
     * 判断用户名和密码是否有效
     *
     * @return
     */
    public boolean isUserNameAndPwdValid() {
        // 用户名和密码不得为空
        if (account.getText().toString().trim().equals("")) {
            Toast.makeText(this, getString(R.string.accountName_empty),
                    Toast.LENGTH_SHORT).show();
            return false;
        } else if (password.getText().toString().trim().equals("")) {
            Toast.makeText(this, getString(R.string.password_empty),
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}